package com.own.component.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * ChildrenUtil
 *
 * @author chenxueli
 * @date 2022-06-15 03:14:15
 */
@Slf4j
public class ChildrenUtil {

    /**
     * 递归处理信息
     *
     * @param list        列表信息
     * @param childrenKey 递归children的key
     * @param doFunction  递归处理函数
     * @param <T>         泛型
     * @author chenxueli
     * @date 2022-06-15 03:14:15
     */
    public static <T> void recursion(
            Collection<T> list,
            Function<T, Collection<T>> childrenKey,
            Consumer<T> doFunction
    ) {
        list.forEach(item -> {
            // 执行信息
            doFunction.accept(item);
            // 继续往下递归信息
            var children = childrenKey.apply(item);
            if (children != null && !children.isEmpty()) {
                recursion(children, childrenKey, doFunction);
            }
        });
    }


}
